<?php
namespace source\core\front;

use Yii;
use app\Models\User;
use source\models\search\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\db\ActiveRecord;
use yii\base\Model;
use yii\helpers\Json;
use yii\helpers\FormatterAll ;
use yii\web\Response ;
use source\core\base\BaseController;


class FrontController extends BaseController
{
    public function  apiResponse($data='error',$format=Response::FORMAT_JSON,$httpState = 200)
    {
        $ret = 1;
        if(empty($data)){
            $ret = -1;
        }
        // validate format
        if(!in_array($format,[Response::FORMAT_JSON,Response::FORMAT_HTML,Response::FORMAT_JSONP,Response::FORMAT_XML,Response::FORMAT_RAW])){
            $format = Response::FORMAT_JSON ;
        }
        Yii::$app->response->setStatusCode($httpState);
        $header = Yii::$app->response->headers ;
        // header set
        $header->set('Content-Type', $format);
        // response
        $response = Yii::$app->response ;
        // gen content
        $handler = new FormatterAll(); // formatter
        $row = ['id'=>time(),'state'=>200,'data'=>$data] ;
        $content = $handler->format($row,$format);
        // set response
        $response->content = $content ;
        $response->send();
        if(!YII_DEBUG ){
            Yii::$app->end($ret);
        }
    }
}
